<template>
  <div id="folk">
    <div class="adress" v-if="selRoleResidential != {}" @click="getRole(2)">
      <md-icon class="icon" name="location" />
      <p class="desc">{{residentialAddress}}</p>
      <span>({{roleTypeTitle}})</span>
    </div>
    <div class="scroll-item" v-if="voteDetails != {}">
      <div class="top">
        <span class="id">业主提议编号：{{voteDetails.number}}</span>
        <span class="status">征集中</span>
      </div>
      <div class="mid">
        <h3>{{voteDetails.title}}</h3>
        <div class="content">
          <p class="desc">{{voteDetails.content}}</p>
          <div class="progress">
            <md-progress :size="50" :value="voteDetails.vote_per * 0.01" :width="5" :rotate="-90">
              <span class="progress-value">{{voteDetails.vote_per}}%</span>
            </md-progress>
            <p class="pr-tip">{{voteDetails.agere_vote}}位业主已支持</p>
          </div>
        </div>
      </div>
      <div class="bottom">
        <div class="op">
          <div class="dow" @click="download(voteDetails.number)">
            <i></i>
            <span>详图下载</span>
          </div>
        </div>
        <p class="time">征集时效至：{{voteDetails.days}}</p>
      </div>
      <div class="btn-blcok">
        <div class="btn" v-if="voteDetails.join == 0" @click="showAgereVote(voteDetails.id,voteDetails.number)">赞成</div>
        <div class="btn no-join" v-if="voteDetails.join == 1">您已投赞成</div>
      </div>
    </div>
    <p class="err-tip" v-else>您的身份不能参与投票</p>
    <div class="masking" v-if="masking && mobileType == 1">
      <i></i>
    </div>
    <div class="masking" v-if="masking && mobileType == 2">
      <div
        class="copy-link"
        v-clipboard:copy="url"
        v-clipboard:success="onCopy"
        v-clipboard:error="onError"
      >复制链接</div>
      <p>复制链接后，发送至微信好友。</p>
    </div>
    <!-- 验证密码 -->
    <div class="verify-vote-layer" v-if="showVoteLayer">
      <p class="error-text" v-if="isErrorText">{{errorText}}</p>
      <div class="inner-content">
        <h3>请验证您的密码</h3>
        <p class="tip">{{firstPassword}}~是您的密码首字母</p>
        <div class="sel-items">
          <input type="password" v-model="passwordValue" placeholder="请输入您的密码" />
        </div>
        <p class="modify-password">
          <span @click="nvaModifyPassword">忘记密码？</span>
        </p>
        <div class="btn-block">
          <div class="cancel" @click="showVoteLayer = false">取消</div>
          <div class="confirm" @click="confirmVote">确定</div>
        </div>
      </div>
    </div>
    <!-- 选角色 -->
    <div class="sel-role-layer" v-if="selRoleLayer">
      <div class="inner-content">
        <h3>请选择角色</h3>
        <div class="sel-items">
          <md-radio
            class="item"
            v-for="(item,index) in roleList"
            :key="index"
            :name="item.rid"
            v-model="rid"
            :label="item.title"
            size="lg"
          />
        </div>
        <div class="btn" @click="comifrmRole">确定</div>
      </div>
    </div>
    <!-- 选小区 -->
    <div class="sel-residential-layer" v-if="selResidentialLayer">
      <div class="inner-content">
        <h3>请选择小区</h3>
        <div class="sel-items">
          <md-radio
            class="item"
            v-model="qid"
            :name="item.qid"
            :label="item.community"
            size="lg"
            v-for="(item,index) in residentialList"
            :key="index"
          />
        </div>
        <div class="btn-block">
          <!-- <div class="cancel" @click="selResidentialLayer = false">取消</div> -->
          <div class="comifrm" @click="comifrmResidential">确定</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Toast, Dialog } from "mand-mobile";
import https from "@/https";
import config from "@/config";
export default {
  components: {
    [Dialog.name]: Dialog
  },
  name: "",
  data() {
    return {
      url: "https://xm.sqyzdh.com/",
      mobileType: 1, //
      masking: false,
      voteDetails: {}, //小区信息
      queryData: {}, //链接参数

      firstPassword: "", //密码首字
      passwordValue: "", //验证密码
      showVoteLayer: false, // 投票验证框
      isErrorText: false, //密码验证错误弹窗
      errorText: "密码输入有误", //错误文本
      roleList: [], //角色列表
      selRoleLayer: false, // 选角色弹窗
      selRoleResidential: {}, //选中的小区
      residentialList: [], //拥有的小区
      selResidentialLayer: false, //选择小区弹窗
      qid: 0, // 选中的小区id
      rid: 2 //
    };
  },
  computed: {
    roleTypeTitle() {
      let roleList = this.roleList;
      let str = "";
      roleList.forEach(item => {
        if (item.rid == this.rid) {
          str = item.title;
        }
      });
      return str;
    },
    residentialAddress() {
      let selRoleResidential = this.selRoleResidential;
      let residentialList = this.residentialList;
      let str = "";
      residentialList.forEach(item => {
        if (item.qid == this.qid) {
          str = item.community;
        }
      });
      str = str || selRoleResidential.community;
      return str;
    }
  },
  methods: {
    onCopy() {
      Toast.succeed("复制成功", 1500);
    },
    onError() {
      Toast.failed("复制失败", 2000);
    },
    shareVote() {
      let number = this.queryData.number;
      let qid = this.queryData.qid;
      let params = {
        number,
        qid,
        uid: this.$store.state.userInfo.id
      };
      https
        .fetchPost(config.shareVote, params)
        .then(res => {
          if (res.data.code == 1) {
            console.log(params, "分享提议提交成功");
          } else {
            console.log(params, res.data.msg);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    getVoteDetails() {
      let data = JSON.parse(localStorage.getItem("saveFolkParams"));
      if (!data) {
        let number = this.$route.query.number;
        let qid = this.$route.query.qid;
        data = {
          number,
          qid,
        };
      }
      data.uid = this.$store.state.userInfo.id;
      this.queryData = data;
      let params = data;
      console.log('有',data)
      https
        .fetchPost(config.voteDetails, params)
        .then(res => {
          if (res.data.code == 1) {
            res.data.data[0].days = this.dataFormat(
              res.data.data[0].days * 3600 * 24 * 1000 +
                res.data.data[0].createtime * 1000,
              2
            );
            this.voteDetails = res.data.data[0];
          } else {
            Toast.failed(res.data.msg, 2000);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    //获取地址栏的参数
    getUrlParam(name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      var r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return null;
    },
    // 下载
    download(number) {
      this.$router.push({
        path: "/download",
        query: { number: number }
      });
    },
    // 时间戳转换
    dataFormat(num, type) {
      var dd = new Date(num);
      var y = dd.getFullYear();
      var m = dd.getMonth() + 1;
      var d = dd.getDate();
      var h = dd.getHours();
      var i = dd.getMinutes();
      var s = dd.getSeconds();
      m = m < 10 ? "0" + m : m;
      d = d < 10 ? "0" + d : d;
      h = h < 10 ? "0" + h : h;
      i = i < 10 ? "0" + i : i;
      s = s < 10 ? "0" + s : s;
      if (type == 1) {
        return y + "-" + m + "-" + d + " " + h + ":" + i + ":" + s;
      } else {
        return y + "-" + m + "-" + d + " " + h + ":" + i;
      }
    },
    // 赞成
    showAgereVote(id, number) {
      this.id = id;
      this.number = number;
      this.firstPassword = this.$store.state.userInfo.acronym_password;
      this.showVoteLayer = true;
      this.passwordValue = "";
    },
    // 提交投票信息
    confirmVote() {
      let params = {
        mobile: this.$store.state.userInfo.mobile,
        id: this.voteDetails.id,
        qid: this.$store.state.residentialType.qid,
        number: this.voteDetails.number,
        password: this.passwordValue
      };
      https
        .fetchPost(config.agereVote, params)
        .then(res => {
          console.log(res.data);
          if (res.data.code == 1) {
            Toast.succeed(res.data.msg);
            this.showVoteLayer = false;
            this.getVoteDetails();
          } else {
            this.isErrorText = true;
            this.errorText = res.data.msg;
            setTimeout(() => {
              this.isErrorText = false;
            }, 2500);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 忘记密码
    nvaModifyPassword() {
      this.$router.push({
        path: "/modifyPassword"
      });
    },
    // 获取角色列表
    getRole(type) {
      let roleType = this.$store.state.roleType; //选中角色
      let rid = roleType.rid;
      let roleList = this.$store.state.userInfo.roleList; //角色列表
      this.roleList = roleList;
      this.initType = type;
      // if (type == 1) {
      //   if (rid == "" || rid == undefined) {
      //     if (roleList.length == 1) {
      //       this.rid = roleList[0].rid;
      //       this.$store.commit("saveRoleType", roleList[0]);
      //       this.getRoleResidential();
      //     } else if (roleList.length > 1) {
      //       this.selRoleLayer = true;
      //       this.rid = roleList[0].rid;
      //     }
      //   } else {
      //     this.rid = rid;
      //     this.getRoleResidential();
      //   }
      // } else if (type == 2) {
      this.selRoleLayer = true;
      this.rid = roleType.rid;
      // }
    },
    // 确认角色
    comifrmRole() {
      let rid = this.rid;
      let roleList = this.roleList;
      roleList.forEach(item => {
        if (rid == item.rid) {
          this.$store.commit("saveRoleType", item);
        }
      });
      if (rid != 2) {
        Toast.failed("角色不正确，只有业主才能投票", 2000);
        return;
      }
      this.getRoleResidential();
      this.selRoleLayer = false;
    },
    // 确认小区
    comifrmResidential() {
      let qid = this.qid;
      let residentialList = this.residentialList;
      console.log("小区id：", this.queryData.qid, "选择小区id：", qid);
      if (qid != this.queryData.qid) {
        Toast.info("相同小区才能进行提议投票", 2000);
        return;
      }
      residentialList.forEach(item => {
        if (qid == item.qid) {
          this.selRoleResidential = item;
          this.$store.commit("saveResidentialType", item);
          this.getVoteDetails();
        }
      });
      this.selResidentialLayer = false;
      this.shareVote();
    },
    // 获取小区列表
    getRoleResidential() {
      Toast.loading("加载中...");
      // let type = this.initType;
      // let residentialType = this.$store.state.residentialType; //选中小区
      // let residentialList = this.$store.state.residentialList; //小区
      // let qid = residentialType.qid;
      // if (type == 1) {
      //   if (qid == "" || qid == undefined) {
      //     let uid = this.$store.state.userInfo.id;
      //     let rid = this.rid;
      //     if (rid == 1) {
      //       Toast.hide();
      //       this.selRoleResidential = {
      //         qid: 1,
      //         community: "产权认证中"
      //       };
      //     } else {
      //       let params = {
      //         uid,
      //         rid
      //       };
      //       https
      //         .fetchGet(config.getRoleResidential, params)
      //         .then(res => {
      //           Toast.hide();
      //           if (res.data.code == 1) {
      //             let residentialList = res.data.data.residentialList;
      //             this.residentialList = residentialList;
      //             this.$store.commit("saveResidentialList", residentialList);
      //             if (residentialList.length == 1) {
      //               this.qid = residentialList[0].qid;
      //               this.selRoleResidential = residentialList[0];
      //               this.$store.commit(
      //                 "saveResidentialType",
      //                 residentialList[0]
      //               );
      //             } else if (residentialList.length > 1) {
      //               this.qid = residentialList[0].qid;
      //               this.selResidentialLayer = true;
      //             } else {
      //               Toast.failed("您尚未有小区信息");
      //             }
      //           } else {
      //             this.$store.commit("saveResidentialType", {});
      //             this.selRoleResidential = {};
      //             Toast.failed(res.data.msg);
      //           }
      //         })
      //         .catch(err => {
      //           Toast.hide();
      //           console.log(err);
      //         });
      //     }
      //   } else {
      //     Toast.hide();
      //     this.qid = qid;
      //     this.selRoleResidential = this.$store.state.residentialType;
      //   }
      // } else if (type == 2) {
      let uid = this.$store.state.userInfo.id;
      let rid = this.rid;
      // if (rid == 1) {
      //   Toast.hide();
      //   this.selRoleResidential = {
      //     qid: 1,
      //     community: "产权认证中"
      //   };
      // } else {
      let params = {
        uid,
        rid
      };
      https
        .fetchGet(config.getRoleResidential, params)
        .then(res => {
          Toast.hide();
          if (res.data.code == 1) {
            let residentialList = res.data.data.residentialList;
            this.residentialList = residentialList;
            this.$store.commit("saveResidentialList", residentialList);
            // if (qid == "" || qid == undefined) {
            this.selRoleResidential = residentialList[0];
            this.qid = residentialList[0].qid;
            // } else {
            //   this.qid = qid;
            //   this.selRoleResidential = this.$store.state.residentialType;
            // }
            this.selResidentialLayer = true;
          }
          // else {
          //   this.$store.commit("saveResidentialType", {});
          //   this.selRoleResidential = {};
          //   Toast.failed(res.data.msg);
          // }
        })
        .catch(err => {
          Toast.hide();
          console.log(err);
        });
    }
    // }
    // }
  },
  created() {
    console.log("shareSuggest:", this.$store.state.shareSuggest);

    this.url = location.href;
    var u = navigator.userAgent,
      app = navigator.appVersion;
    var isAndroid = u.indexOf("Android") > -1 || u.indexOf("Linux") > -1; //g
    var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if (isAndroid) {
      this.mobileType = 1;
      console.log("这个是安卓操作系统");
      //这个是安卓操作系统
    }
    if (isIOS) {
      this.mobileType = 2;
      console.log("这个是ios操作系统"); //这个是ios操作系统
    }
    if (this.$store.state.shareSuggest) {
      this.masking = true;
      // this.getVoteDetails();
      // this.getRole(1);
    } else {
      Toast.loading("加载中...");
      let number = this.$route.query.number;
      let qid = this.$route.query.qid;
      let folkParams = {
        number,
        qid
      };
      if (JSON.stringify(folkParams) != "{}") {
        localStorage.setItem("saveFolkParams", JSON.stringify(folkParams));
      }
      var code = this.getUrlParam("code"); // 截取路径中的code，如果没有就去微信授权，如果已经获取到了就直接传code给后台获取openId
      var local = encodeURIComponent("https://xm.sqyzdh.com/shareSuggest");
      var APPID = "wx8d06ea96180136be";
      if (code == null || code === "" || code == this.$store.getters.getCode) {
        window.location.href =
          "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
          APPID +
          "&redirect_uri=" +
          local +
          "&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect";
      } else {
        this.$store.commit("saveCode", code);
        let params = {
          code
        };
        https
          .fetchGet(config.getLoginStatus, params)
          .then(res => {
            Toast.hide();
            if (res.data.code == 1) {
              let userInfo = res.data.data;
              this.$store.commit("saveUserInfo", userInfo);
              this.getVoteDetails();
              this.getRole(1);
            } else if (res.data.code == 0) {
              let wxInfo = res.data.data;
              this.$store.commit("saveWxInfo", wxInfo);
              this.$router.replace({
                path: "/shareRegister",
                query: {
                  type: 5
                }
              });
            } else {
              Toast.failed(res.data.msg);
            }
          })
          .catch(err => {
            Toast.hide();
            console.log(err);
          });
      }
    }
  },
  mounted() {
    this.$store.commit("saveShareSuggest", false);
    console.log("shareSuggest:", this.$store.state.shareSuggest);
  }
};
</script>

<style  lang="scss" scoped>
$themeColor: #444444;
$themeGold: #d7b987;
#folk {
  padding-top: 40px;
}
.err-tip {
  font-size: 36px;
  text-align: center;
  color: #444;
  margin-top: 160px;
}
.adress {
  display: flex;
  align-items: center;
  font-size: 34px;
  width: 650px;
  height: 60px;
  margin: 0 30px 30px;
  padding: 0 20px;
  color: #444;
  background: #f2f2f2;
  .icon {
    font-size: 32px;
    margin-right: 10px;
  }
  .desc {
    line-height: 60px;
    max-width: 480px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
.scroll-item {
  box-shadow: 0 1px 16px #ddd;
  margin: 0 30px 30px;
  padding-bottom: 15px;
  border-radius: 8px;
  .top {
    padding: 0 20px;
    font-size: 24px;
    color: #fff;
    display: flex;
    background: linear-gradient(to right, #5299f5, #2f86f6);
    justify-content: space-between;
    align-items: center;
    height: 70px;
    border-top-left-radius: 8px;
    border-top-right-radius: 8px;
    .status {
      color: #fff;
      font-size: 26px;
    }
  }
  .mid {
    display: flex;
    justify-content: space-between;
    flex-direction: column;
    padding: 0 20px;
    h3 {
      font-size: 30px;
      line-height: 50px;
      padding: 20px 0 0;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .content {
      display: flex;
      justify-content: space-between;
      margin-top: 15px;
      .desc {
        font-size: 26px;
        color: #777;
        line-height: 40px;
        width: 410px;
      }
      .progress {
        display: flex;
        flex-direction: column;
        align-items: center;
        padding: 0 0 10px;
        width: 220px;
        .progress-value {
          font-size: 26px;
          color: #666;
        }
        .pr-tip {
          margin-top: 10px;
          font-size: 24px;
          color: #2f86f6;
          line-height: 35px;
        }
      }
    }
  }
  .bottom {
    display: flex;
    padding: 0 20px;
    justify-content: space-between;
    margin-top: 10px;
    .op {
      display: flex;
      align-items: center;
      font-size: 26px;
      color: #666;
      i {
        width: 32px;
        height: 32px;
        margin-right: 8px;
      }
      .share {
        display: flex;
        align-items: center;
        height: 100%;
        i {
          background: url("/static/images/suggest/share.png") center center
            no-repeat;
          background-size: 100%;
        }
      }
      .col {
        display: flex;
        align-items: center;
        height: 100%;
        i {
          background: url("/static/images/suggest/col.png") center center;
          background-size: 100%;
        }
      }
      .dow {
        display: flex;
        align-items: center;
        margin-left: 35px;
        height: 100%;
        i {
          background: url("/static/images/suggest/dow.png") center center
            no-repeat;
          background-size: 100%;
        }
      }
    }
    .time {
      font-size: 24px;
      color: #2f86f6;
      line-height: 50px;
    }
  }
  .btn-blcok {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 40px;
    padding-bottom: 20px;
    .btn {
      display: flex;
      align-items: center;
      justify-content: center;
      border: 1px solid #c5cad5;
      width: 200px;
      height: 60px;
      border-radius: 50px;
      font-size: 28px;
      margin: 0 20px;
      color: #444;
      background: #fff;
      &.no-join {
        background: #2f86f6;
        border: 1px solid #2f86f6;
        color: #fff;
      }
    }
  }
}
//  <!-- 验证密码 -->
.verify-vote-layer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 998 !important;
  background: rgba(0, 0, 0, 0.6);
  .error-text {
    position: absolute;
    top: 0;
    width: 100%;
    height: 50px;
    line-height: 50px;
    font-size: 26px;
    color: #fff;
    text-align: center;
    background: #e22c1f;
  }
  .inner-content {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background: #fff;
    width: 560px;
    border-radius: 12px;
    padding: 30px 30px 0;
    h3 {
      display: flex;
      justify-content: center;
      font-size: 32px;
      color: #333;
      padding-bottom: 50px;
      padding-top: 20px;
    }
    .tip {
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 26px;
      height: 60px;
      width: 350px;
      padding: 0 30px;
      color: rgb(226, 11, 11);
    }
    .sel-items {
      display: flex;
      justify-content: center;
      input {
        border: none;
        outline: none;
        width: 350px;
        height: 60px;
        font-size: 30px;
        color: #444;
        background: #f9f9f9;
        padding: 0 20px;
      }
    }
    .modify-password {
      text-align: right;
      color: #2f86f6;
      font-size: 24px;
      padding: 30px 0;
      span {
        padding: 10px;
      }
    }
    .btn-block {
      display: flex;
      border-top: 1px solid #f1f1f1;

      div {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 50%;
        height: 100px;
        color: #2f86f6;
        font-size: 30px;
        &.cancel {
          color: #999;
          position: relative;
          &::after {
            content: "";
            position: absolute;
            top: 20px;
            right: 0;
            width: 1px;
            height: 60px;
            background: #f2f2f2;
          }
        }
      }
    }
  }
}
.masking {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 99;
  display: flex;
  align-items: center;
  flex-direction: column;
  background: rgba(0, 0, 0, 0.9);
  .copy-link {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 400px;
    height: 70px;
    // background: #555;
    background: #2f86f6;
    color: #fff;
    font-size: 32px;
    margin-top: 400px;
    border-radius: 40px;
  }
  p {
    color: #fff;
    font-size: 24px;
    text-align: center;
    margin-top: 25px;
  }
  i {
    width: 555px;
    height: 522px;
    margin-top: 200px;
    background: url("")
      no-repeat;
    background-size: 100%;
  }
}
// 选角色
.sel-role-layer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 11;
  background: rgba(0, 0, 0, 0.6);
  .inner-content {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background: #fff;
    width: 460px;
    border-radius: 12px;
    padding: 30px 30px 0;
    h3 {
      display: flex;
      justify-content: center;
      font-size: 28px;
      color: #666;
      padding-bottom: 30px;
    }
    .sel-items {
      display: flex;
      flex-direction: column;
      padding-bottom: 30px;
      max-height: 400px;
      overflow: auto;
      .item {
        padding-left: 120px;
        height: 60px;
        font-size: 36px;
        color: #444;
      }
    }
    .btn {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100px;
      border-top: 1px solid #f1f1f1;
      color: #2f86f6;
      font-size: 30px;
    }
  }
}
// 选小区
.sel-residential-layer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  z-index: 11;
  background: rgba(0, 0, 0, 0.6);
  .inner-content {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    background: #fff;
    width: 560px;
    border-radius: 12px;
    padding: 30px 30px 0;
    h3 {
      display: flex;
      justify-content: center;
      font-size: 28px;
      color: #666;
      padding-bottom: 30px;
    }
    .sel-items {
      display: flex;
      flex-direction: column;
      padding-bottom: 30px;
      max-height: 400px;
      overflow: auto;
      .item {
        // height: 60px;
        font-size: 36px;
        color: #444;
      }
    }
    .btn-block {
      display: flex;
      border-top: 1px solid #f1f1f1;

      div {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 100%;
        height: 100px;
        color: #2f86f6;
        font-size: 30px;
        &.cancel {
          color: #999;
          position: relative;
          &::after {
            content: "";
            position: absolute;
            top: 20px;
            right: 0;
            width: 1px;
            height: 60px;
            background: #f2f2f2;
          }
        }
      }
    }
  }
}
</style>
